// Author: wsfuyibing <682805@qq.com>
// Date: 2025-06-28

package list

import (
	"context"
	"gitee.com/go-libs/result"
	"gitee.com/go-libs/validator-iris/validator"
	"gitee.com/go-wares/framework-iris/framework/src/logics"
	"github.com/kataras/iris/v12"
	"server/app/errs"
	"server/app/http/models"
	"server/app/http/services"
	"server/app/kind"
	"server/app/lib/authorizer"
)

// Logic
// 渔票列表.
type Logic struct {
	auth     *authorizer.Authentication
	request  *Request
	response []*Response
}

func New() logics.Logical {
	return &Logic{
		request:  &Request{},
		response: make([]*Response, 0),
	}
}

func (o *Logic) Before(_ context.Context, i iris.Context) (err error) {
	if err = validator.Query(i, o.request); err == nil {
		o.auth = authorizer.New().Get(i)
	}
	return
}

func (o *Logic) Run(ctx context.Context, _ iris.Context) result.Result {
	var (
		conditions = map[string]any{
			"user_id": o.auth.UserId,
		}
		err     error
		items   []*models.TicketRecord
		res     = result.New()
		service = services.NewTicketRecordService()
		total   int64

		arcadeId  = make([]kind.ArcadeId, 0)
		seatId    = make([]int64, 0)
		sessionId = make([]int64, 0)
	)

	// 1. 订单列表.
	if items, total, err = service.Paging(ctx, conditions, o.request.Page, o.request.Size); err != nil {
		return res.WithError(errs.ErrDatabaseQuery)
	}

	// 2. 遍历数据.
	for _, item := range items {
		// 2.1 关联钓场.
		if item.ArcadeId.IsBound() {
			arcadeId = append(arcadeId, item.ArcadeId)
		}
		// 2.2 关联钓位.
		if item.SeatId > 0 {
			seatId = append(seatId, item.SeatId)
		}
		// 2.3 关联场次.
		if item.SessionId > 0 {
			sessionId = append(sessionId, item.SessionId)
		}
		// 2.4 结果追加.
		o.response = append(o.response, (&Response{}).With(item))
	}

	// 3. 场次信息.
	if len(sessionId) > 0 {
		if err = o.RunSession(ctx, sessionId); err != nil {
			return res.WithError(err)
		}
	}

	// 4. 绑定钓位.
	if len(seatId) > 0 {
		if err = o.RunSeat(ctx, seatId); err != nil {
			return res.WithError(err)
		}
	}

	// 5. 钓场信息.
	if len(arcadeId) > 0 {
		if err = o.RunArcade(ctx, arcadeId); err != nil {
			return res.WithError(err)
		}
	}

	return res.WithPaging(o.response, total, o.request.Page, o.request.Size)
}

// RunArcade
// 钓场信息.
func (o *Logic) RunArcade(ctx context.Context, ids []kind.ArcadeId) (err error) {
	var mapper map[kind.ArcadeId]*models.Arcades

	// 1. 钓场列表.
	if mapper, err = services.NewArcadesService().MapByIdList(ctx, ids); err != nil {
		return
	}

	// 2. 遍历钓场.
	for _, r := range o.response {
		if v, ok := mapper[r.ArcadeId]; ok {
			r.WithArcade(v)
		}
	}
	return
}

// RunPond
// 鱼塘信息.
func (o *Logic) RunPond(ctx context.Context, ids []int64) (err error) {
	var mapper map[int64]*models.ArcadePond

	// 1. 鱼塘列表.
	if mapper, err = services.NewArcadePondService().MapByIdList(ctx, ids); err != nil {
		return
	}

	// 2. 遍历鱼塘.
	for _, r := range o.response {
		if r.Session != nil {
			if r.Session.Ticket != nil {
				if v, ok := mapper[r.Session.Ticket.PondId]; ok {
					r.Session.Ticket.WithPond(v)
				}
			}
		}
	}
	return
}

// RunSeat
// 钓位信息.
func (o *Logic) RunSeat(ctx context.Context, ids []int64) (err error) {
	var mapper map[int64]*models.ArcadePondSeat

	// 1. 钓位列表.
	if mapper, err = services.NewArcadePondSeatService().MapByIdList(ctx, ids); err != nil {
		return
	}

	// 2. 遍历钓位.
	for _, r := range o.response {
		if v, ok := mapper[r.SeatId]; ok {
			r.WithSeat(v)
		}
	}
	return
}

// RunSession
// 场次信息.
func (o *Logic) RunSession(ctx context.Context, ids []int64) (err error) {
	var (
		tid    = make([]int64, 0)
		mapper map[int64]*models.TicketSession
	)

	// 1. 场次列表.
	if mapper, err = services.NewTicketSessionService().MapByIdList(ctx, ids); err != nil {
		return
	}

	// 2. 遍历场次.
	for _, r := range o.response {
		if v, ok := mapper[r.SessionId]; ok {
			if v.TicketId > 0 {
				tid = append(tid, v.TicketId)
			}
			r.WithSession(v)
		}
	}

	// 3. 绑定渔票.
	if len(tid) > 0 {
		err = o.RunTicket(ctx, tid)
	}
	return
}

// RunTicket
// 渔票信息.
func (o *Logic) RunTicket(ctx context.Context, ids []int64) (err error) {
	var (
		mapper map[int64]*models.Ticket
		pid    = make([]int64, 0)
	)

	// 1. 渔票列表.
	if mapper, err = services.NewTicketService().MapByIdList(ctx, ids); err != nil {
		return
	}

	// 2. 遍历渔票.
	for _, r := range o.response {
		if r.Session != nil {
			if v, ok := mapper[r.Session.TicketId]; ok {
				pid = append(pid, v.PondId)
				r.Session.WithTicket(v)
			}
		}
	}

	// 3. 绑定鱼塘.
	if len(pid) > 0 {
		if err = o.RunPond(ctx, pid); err != nil {
			return
		}
	}
	return
}
